package com.kitty.game.equip.facade;

import com.kitty.game.equip.message.*;
import com.kitty.game.role.model.Role;
import com.kitty.game.equip.model.RoleEquip;
import com.kitty.game.config.Equip;
import com.kitty.common.utils.SpringUtils;
import com.kitty.game.equip.EquipDataPool;
import com.kitty.game.equip.service.EquipService;
import com.kitty.listener.EventDispatcher;
import com.kitty.listener.EventType;
import com.kitty.listener.event.ChangeEquipPageEvent;
import com.kitty.listener.event.UpgradeEquipEvent;
import com.kitty.mina.annotation.RequestMapping;
import com.kitty.mina.cache.SessionUtils;
import com.kitty.mina.task.ThreadLocalUtil;
import org.apache.mina.core.session.IoSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
//后写

@Controller
public class EquipController {
    @Autowired
    EquipService equipService;

    /**法宝补灵气*/
    @RequestMapping
    public void artifactNimbus(IoSession session, ReqRefillArtifactNimbus refillArtifactNimbus) {
        int id = refillArtifactNimbus.getId();
        if (id <= 0) {
            return;
        }
        equipService.refillArtifactNimbus(session, id);
    }

    /**
     * 法宝亲密丹加亲密度
     * @param session
     * @param reqAddFabaoIntimacyCount
     */
    @RequestMapping
    public void reqAddFabaoIntimacyCount(IoSession session, ReqAddFabaoIntimacyCount reqAddFabaoIntimacyCount) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }
        if(reqAddFabaoIntimacyCount.getPos()<=0){
            return;
        }
        if(reqAddFabaoIntimacyCount.getNum()<=0){
            return;
        }
        int num  = equipService.getCount("法宝亲密丹",false,role);
        if(num < reqAddFabaoIntimacyCount.getNum()){
            return;
        }
        equipService.refillArtifactIntimacy(role, reqAddFabaoIntimacyCount.getPos(), reqAddFabaoIntimacyCount.getNum());
    }

    @RequestMapping
    public void freUpgradeEquip(IoSession session, ReqFreUpgradeEquip reqFreUpgradeEquip) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) return;
        RoleEquip roleEquip = SpringUtils.getBean(EquipService.class).getRoleEquipByPos(role, reqFreUpgradeEquip.getPos());
        Equip equip = EquipDataPool.getByName(roleEquip.getName());
        if (equip == null) {
            return;
        }
        equipService.equipPreview(role,equip,roleEquip,reqFreUpgradeEquip.getType());
    }

    @RequestMapping
    public void upgradeEquip(IoSession session, ReqUpgradeEquip upgradeEquip) {
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }
        /**产生一个装备打造的事件*/
        ThreadLocalUtil.addLocalTask(role, () -> EventDispatcher.getInstance().fireEvent(new UpgradeEquipEvent(EventType.UPGRADE_EQUIP, role, upgradeEquip)));
    }


    @RequestMapping
    public void switchBakEquip(IoSession session , ReqSwitchBackEquip reqSwitchBackEquip){
        Role role = SessionUtils.getRoleBySession(session);
        if (role == null) {
            return;
        }
        /**产生一个切换装备页事件*/
        ThreadLocalUtil.addLocalTask(role, () -> EventDispatcher.getInstance().fireEvent(new ChangeEquipPageEvent(EventType.CHANGE_EQUIP_PAGE, role)));
    }

    @RequestMapping
    public void quickUseItem(IoSession session, ReqQuickUseItem reqQuickUseItem) {
        Role role = SessionUtils.getRoleBySession(session);
        SpringUtils.getEquipService().quickUseItem(role, reqQuickUseItem.getPos(), reqQuickUseItem.getDoubleEnabel(), reqQuickUseItem.getChongfsEnable());
    }
}
